'use strict';

import path from 'path';
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import MappingPlugin from 'webpack-mapping-plugin';
import precss from 'precss';
import autoprefixer from 'autoprefixer';

const webpackConfig = {
  entry: {
    panel: ['./client/pages/panel.js']
  },

  output: {
    path: path.join(__dirname, 'public/dist/'),
    filename: '[name].[chunkhash].js',
    publicPath: '/public/dist/'
  },

  resolve: {
    modules: [
      path.join(__dirname, "client"),
      "node_modules"
    ],
    //模块别名定义，方便直接引用别名
    alias: {
      'react-router-redux': path.resolve(__dirname, 'node_modules/react-router-redux-fixed/lib/index.js'),
    }
  },

  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)/,
        loader: 'url-loader?name=[hash].[ext]&limit=8192'
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?localIdentName=[name]_[local]_[hash:base64:5]!postcss-loader?pack=cleaner'
        })
      },
      {
        test: /\.less$/,
        exclude: /node_modules/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?localIdentName=[name]_[local]_[hash:base64:5]!postcss-loader?pack=cleaner!less-loader'
        })
      },
      {
        test: /\.(mp4|ogg)$/,
        loader: 'file-loader'
      },
      {
        test: /\.hbs$/,
        loader: "handlebars"
      }
    ]
  },

  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor']
    }),
    new ExtractTextPlugin({
      filename: 'css/[name].[chunkhash].css',
      allChunks: true,
      disable: false
    }),
    new MappingPlugin({
      fileName: 'mapping.json',
      basePath: ''
    }),
    new webpack.optimize.UglifyJsPlugin({
      compressor: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      options: {
        postcss () {
          return {
            defaults: [precss, autoprefixer],
            cleaner: [autoprefixer({
              browsers: ['Android >= 4.0', 'iOS >=7']
            })]
          };
        }
      }
    })
  ]
};

export default webpackConfig;